package com.gupt.weekly.controller;

import com.gupt.weekly.domain.Plan;
import com.gupt.weekly.service.PlanService;
import com.gupt.weekly.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author linzizun
 * @version 1.0
 * @date 2021/03/12 16:49
 */
@Slf4j
@RestController
@Api(tags = "周报计划模块接口")
@RequestMapping("/plan")
public class PlanController {
    @Resource
    PlanService planService;

    @ApiOperation(value = "创建计划", notes = "创建周报计划")
    @PostMapping(value = "/createPlan")
    public CommonResult createPlan(@Valid @RequestBody Plan plan) {
        plan.setId(null);
        plan.setRelease_time(null);
        plan.setHas_release(0);
        int result = planService.insert(plan);
        log.info("*****插入结果：" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功", result);
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @ApiOperation(value = "查询计划", notes = "根据id查询周报计划")
    @GetMapping(value = "/getPlanById")
    public CommonResult getPlanById(String id) {
        Plan plan = planService.selectById(id);
        if (null != plan) {
            return new CommonResult(200, "查询数据库成功", plan);
        } else {
            return new CommonResult(444, "查询数据库失败，此id不存在");
        }
    }

    @ApiOperation(value = "获取计划列表", notes = "获取周报计划列表")
    @GetMapping(value = "/getPlanList")
    public CommonResult getPlanList(@RequestBody Plan plan) {
        List<Plan> planList = planService.selectList(plan);
        if (planList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", planList);
        } else {
            return new CommonResult(444, "数据库无相关记录");
        }
    }

    @ApiOperation(value = "获取未开始计划列表", notes = "获取未开始周报计划列表")
    @GetMapping(value = "/getNotStartList")
    public CommonResult getNotStartList(@RequestBody Plan plan) {
        List<Plan> planList = planService.selectNotStartList(plan);
        if (planList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", planList);
        } else {
            return new CommonResult(444, "数据库无相关记录");
        }
    }

    @ApiOperation(value = "获取未结束计划列表", notes = "获取未结束周报计划列表")
    @GetMapping(value = "/getNotClosedList")
    public CommonResult getNotClosedList(@RequestBody Plan plan) {
        List<Plan> planList = planService.selectNotClosedList(plan);
        if (planList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", planList);
        } else {
            return new CommonResult(444, "数据库无相关记录");
        }
    }

    @ApiOperation(value = "获取进行中的计划列表", notes = "获取进行中的周报计划列表")
    @GetMapping(value = "/getOngoingList")
    public CommonResult getOngoingList(@RequestBody Plan plan) {
        List<Plan> planList = planService.selectOngoingList(plan);
        if (planList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", planList);
        } else {
            return new CommonResult(444, "数据库无相关记录");
        }
    }

    @ApiOperation(value = "获取已结束计划列表", notes = "获取已结束周报计划列表")
    @GetMapping(value = "/getHasClosedList")
    public CommonResult getHasClosedList(@RequestBody Plan plan) {
        List<Plan> planList = planService.selectHasClosedList(plan);
        if (planList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", planList);
        } else {
            return new CommonResult(444, "数据库无相关记录");
        }
    }

    @ApiOperation(value = "更改计划", notes = "根据id更改周报计划")
    @PostMapping(value = "/updatePlanById")
    public CommonResult updatePlanById(@Valid @RequestBody Plan plan) {
        int result = planService.updateById(plan);
        if (result > 0) {
            return new CommonResult(200, "更新数据库成功", result);
        } else {
            return new CommonResult(444, "更新数据库失败");
        }
    }

    @ApiOperation(value = "发布计划", notes = "根据id发布填写完的周报计划")
    @GetMapping(value = "/releasePlanById")
    public CommonResult releasePlanById(String id) {
        Plan plan = new Plan();
        plan.setId(id);
        plan.setRelease_time(new Date());
        plan.setHas_release(1);
        int result = planService.updateById(plan);
        if (result > 0) {
            return new CommonResult(200, "更新数据库成功", result);
        } else {
            return new CommonResult(444, "更新数据库失败");
        }
    }
}
